<template>
  <div class="container">
    <el-dialog title="修改标签" :visible.sync="addTagsDialogVisible" width="20%" @close="dialogClose">
      <el-form label="right" label-width="80px" :model="addForm" :rules="rules" ref="addfrom">
        <el-form-item label="所属学科" prop="subjectID">
          <el-select placeholder="请选择" v-model="addForm.subjectID">
            <el-option
              v-for="(item, i) in subjectList"
              :key="i"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model.trim="addForm.tagName" placeholder="请输入标签名"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addTagsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    subjectList: {
      type: Array,
      required: true,
    },
    row: {
      type: Object,
      required: true,
    },
  },
  name: "EditDialog",
  mounted() {},
  data() {
    return {
      addTagsDialogVisible: false,
      addForm: {
        subjectID: null,
        tagName: '',
      },
      rules: {
        tagName: [
          { required: true, message: "请输入标签名称", trigger: "change" },
        ],
        subjectID: [{ required: true, message: "请选择", trigger: "change" }],
      },
    }
  },
  methods: {
    dialogClose() {
      this.$refs.addfrom.resetFields()
    },
    add() {
      // 预验证
      this.$refs.addfrom.validate((valid) => {
        if (!valid) return
        this.$emit("update", this.addForm)
      })
    },
  },
  watch: {
    "row":{
      handler:function (val) {
        // console.log(val)
        this.addForm.subjectID = val.subjectID
        this.addForm.tagName = val.tagName
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style scoped lang='less'>
.el-select {
  width: 100%
}
</style>
